home *** CD-ROM | disk | FTP | other *** search
- #include "graphics.h"
- #include "main.h"
- #include "apple events.h"
- #include "integrity.h"
- #include "about.h"
- #include "menus.h"
- #include "prefs.h"
- #include "environment.h"
- #include "error.h"
- #include "print meat.h"
- #include "kant.h"
- #include "program globals.h"
- #include "AppleEvents.h"
- #include "EPPC.h"
-
- static short gTheCurrentModifiers;
-
- void main(void)
- {
- Boolean programIntegrityVerified;
- Boolean programIntegritySet;
-
- /* do integrity check before anything else; see integrity.c for details */
- programIntegrityVerified=DoIntegrityCheck(&programIntegritySet);
-
- /* standard program initialization stuff */
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- GetDateTime((unsigned long*)&qd.randSeed);
-
- if (!InitTheEnvironment()) /* gestalt checks and variable initialization */
- HandleError(kSystemTooOld, TRUE); /* less than system 4.1 */
-
- if (!programIntegrityVerified) /* integrity check failed */
- HandleError(kProgramIntegrityNotVerified, TRUE);
-
- if (programIntegritySet) /* integrity check freshly installed */
- HandleError(kProgramIntegritySet, FALSE);
-
- PrefsError(PreferencesInit()); /* get prefs (create if necessary) */
-
- if (!InitTheGraphics()) /* initialize offscreen gworlds/bitmaps, etc */
- HandleError(kNoMemoryAndQuitting, TRUE);
-
- if (!InitTheMenus()) /* get menus from .rsrc and draw menu bar */
- HandleError(kProgramIntegrityNotVerified, TRUE);
-
- InitThePrinting();
-
- InitTheProgram(); /* program-specific initialization */
-
- EventLoop(); /* where it all happens (see below) */
-
- ShutDownEnvironment(TRUE); /* where it all ends (see below) */
-
- ExitToShell();
- }
-
- void EventLoop(void)
- {
- while (!gDone) /* gDone set by choosing "Quit" menu item or by "quit" apple event */
- HandleSingleEvent(TRUE);
- }
-
- Boolean HandleSingleEvent(Boolean allowContextSwitching)
- {
- EventRecord theEvent;
- short i;
-
- if (!gCustomCursor)
- SetCursor(&qd.arrow); /* should set once every time through event loop */
- HiliteMenu(0); /* normalize menubar */
-
- gFrontWindowIsOurs=FALSE;
- gFrontWindowIndex=0;
- if (FrontWindow()!=0L) /* if there's a front window, see if it's one of ours */
- {
- for (i=0; ((i<NUM_WINDOWS) && (!gFrontWindowIsOurs)); i++)
- {
- if (FrontWindow()==GetIndWindowPtr(i)) /* found one of ours, so SetPort */
- {
- SetPort(GetIndWindowPtr(i));
- gFrontWindowIsOurs=TRUE;
- gFrontWindowIndex=i;
- }
- }
- }
-
- /* get an event from the queue */
- WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? gBackgroundWaitTime :
- ((gKludgeIter<3) ? 0 : gForegroundWaitTime), 0L);
- gTheCurrentModifiers=theEvent.modifiers;
-
- DispatchEvents(theEvent, allowContextSwitching); /* handle the event we just got */
-
- return (theEvent.what!=nullEvent);
- }
-
- short GetTheModifiers(void)
- {
- return gTheCurrentModifiers;
- }
-
- void DispatchEvents(EventRecord theEvent, Boolean allowContextSwitching)
- {
- short i;
- Point thisPoint;
- short index;
- WindowPtr theWindow;
- Boolean thisWindowIsOurs;
-
- thisWindowIsOurs=FALSE;
- /* for update/activate events, see if the window in question is one of ours */
- if ((theEvent.what==activateEvt) || (theEvent.what==updateEvt))
- {
- for (i=0; ((i<NUM_WINDOWS) && (!thisWindowIsOurs)); i++)
- thisWindowIsOurs=((WindowPtr)theEvent.message==GetIndWindowPtr(i));
- }
-
- if (thisWindowIsOurs) /* for activate/update events, get window index */
- index=(**((WindowDataHandle)GetWRefCon((WindowPtr)theEvent.message))).windowIndex;
- else if (gFrontWindowIsOurs) /* if front window is ours, get its window index */
- index=gFrontWindowIndex;
- else index=-1;
-
- switch (theEvent.what)
- {
- case nullEvent: /* ain't nuthin' happenin' */
- if (gKludgeIter<3)
- {
- gKludgeIter++;
- }
- else
- {
- if (gNeedToOpenWindow)
- {
- OpenTheIndWindow(kMainWindow);
- gNeedToOpenWindow=FALSE;
- }
- }
-
- if (gFrontWindowIsOurs) /* give control to window dispatch to handle null */
- CallIndDispatchProc(index, kNull, 0L);
- break;
- case mouseDown: /* mouse button pressed */
- HandleMouseDown(theEvent, allowContextSwitching); /* see below for mousedown handling */
- break;
- case keyDown: /* key pressed */
- case autoKey: /* key help down */
- if (theEvent.modifiers & cmdKey) /* handle as command-key equivalent */
- {
- AdjustMenus(); /* just to be safe */
- /* get the menu ID + item and handle it as a menu choice */
- HandleMenu(MenuKey((char)(theEvent.message & charCodeMask)));
- }
- else if (gFrontWindowIsOurs) /* --> window's dispatch for keydown */
- CallIndDispatchProc(index, kKeydown, theEvent.message);
- break;
- case diskEvt: /* disk insert */
- if (HiWord(theEvent.message)!=noErr) /* bad disk inserted */
- {
- DILoad(); /* load disk initialization package */
- SetPt(&thisPoint, 120, 120);
- DIBadMount(thisPoint, theEvent.message); /* give format? dialog */
- DIUnload(); /* unload 'cuz we certainly don't need it */
- }
- break;
- case updateEvt: /* window update */
- theWindow=(WindowPtr)theEvent.message; /* which window? */
-
- BeginUpdate(theWindow); /* means: "OK, we're dealing with this now" */
-
- if (thisWindowIsOurs) /* one of ours? see graphics.c */
- UpdateTheWindow(GetWindowDataHandle(index));
-
- EndUpdate(theWindow); /* means: "OK, we're done updating now" */
- break;
- case activateEvt: /* window activate or deactivate */
- if (thisWindowIsOurs) /* one of ours? send message to window dispatch */
- CallIndDispatchProc(index, ((theEvent.modifiers&activeFlag)!=0) ?
- kActivate : kDeactivate, 0L);
- break;
- case osEvt: /* suspend or resume program execution (switch in/out) */
- if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
- {
- /* keep track of whether we're in the background or foreground */
- gIsInBackground=((theEvent.message&resumeFlag)==0);
-
- if (gFrontWindowIsOurs) /* send activate/deactivate to front window */
- CallIndDispatchProc(index, gIsInBackground ? kDeactivate : kActivate, 0L);
-
- for (i=0; i<NUM_WINDOWS; i++) /* send suspend/resume to all our */
- if (GetIndWindowPtr(i)!=0L) /* windows that currently exist */
- CallIndDispatchProc(i, gIsInBackground ? kSuspend : kResume, 0L);
-
- /* if we just came into the foreground and we have a pending error,
- now's the time to display it */
- if ((!gIsInBackground) && (gPendingResultCode!=allsWell))
- {
- if (gHasNotificationManager)
- NMRemove(&gMyNotification); /* remove notification request */
- HandleError(gPendingResultCode, FALSE); /* display alert, see error.c */
- gPendingResultCode=allsWell; /* ...now it is */
- }
- }
- break;
- case kHighLevelEvent: /* apple event */
- AEProcessAppleEvent(&theEvent); /* see apple events.c */
- break;
- }
- }
-
- void HandleMouseDown(EventRecord theEvent, Boolean allowContextSwitching)
- {
- WindowPtr theWindow;
- short windowCode;
- long windSize;
- GrafPtr oldPort;
- Rect sizeRect;
- short index;
- unsigned long dummy;
- Point theLocalPoint;
- Boolean thisWindowIsOurs;
- ExtendedWindowDataHandle theData;
-
- windowCode=FindWindow(theEvent.where, &theWindow); /* which window? */
-
- thisWindowIsOurs=FALSE;
- /* find out if the target window was one of ours */
- if (theWindow!=0L)
- for (index=0; ((index<NUM_WINDOWS) && (!thisWindowIsOurs)); index++)
- {
- theData=GetWindowDataHandle(index);
- thisWindowIsOurs=(theWindow==GetWindowPtr(theData));
- }
-
- if (thisWindowIsOurs) /* if target window is one of ours, get window data struct */
- {
- index=GetWindowIndex(theData);
- }
- else index=-1;
-
- switch (windowCode)
- {
- case inMenuBar: /* in menu bar; let system take over */
- AdjustMenus();
- HandleMenu(MenuSelect(theEvent.where));
- break;
- case inContent: /* in window content */
- if (FrontWindow() != theWindow) /* maybe switch to different window */
- {
- // if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
- if (0)
- {
- SysBeep(7);
- return;
- }
-
- SelectWindow(theWindow);
- }
- else if (gFrontWindowIsOurs) /* inform window dispatch of mousedown */
- {
- theLocalPoint=theEvent.where;
- GlobalToLocal(&theLocalPoint);
- dummy=theLocalPoint.h; /* all this fiddling is so we can pass */
- dummy=dummy<<16; /* the point (two integers) to the */
- dummy+=theLocalPoint.v; /* dispatch procedure in an unsigned long */
- CallIndDispatchProc(index, kMousedown, dummy); /* go for it */
- }
- break;
- case inSysWindow: /* in system window (desk accessory) */
- if (allowContextSwitching)
- SystemClick(&theEvent, theWindow); /* let the system deal with it */
- break;
- case inDrag: /* in drag _region_, that is */
- /* the accepted way to draw a window */
- // if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
- if (0)
- {
- SysBeep(7);
- return;
- }
- DragWindow(theWindow, theEvent.where, &((**GetGrayRgn()).rgnBBox));
- if (thisWindowIsOurs) /* update window bounds in window data struct */
- {
- if (CallDispatchProc(theData, kDrag, 0L)==kFailure)
- {
- SetWindowData_windowBounds(theData,
- (*(((WindowPeek)GetWindowPtr(theData))->contRgn))->rgnBBox);
- SetWindowData_windowPos(theData,
- GetWindowBoundsTopLeft((WindowDataHandle)theData));
- SetWindowData_windowPosInitted(theData, TRUE);
- }
- }
- break;
- case inGoAway: /* close box */
- /* the accepted way to track a close box attempt */
- // if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
- if (0)
- {
- SysBeep(7);
- return;
- }
-
- if (TrackGoAway(theWindow, theEvent.where))
- DoTheCloseThing((WindowPeek)theWindow); /* see menus.c */
- break;
- case inGrow: /* grow box */
- /* the accepted way to grow a window */
- // if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
- if (0)
- {
- SysBeep(7);
- return;
- }
-
- if (thisWindowIsOurs)
- {
- if (CallDispatchProc(theData, kGetGrowSize, (unsigned long)&sizeRect)==kFailure)
- sizeRect=qd.screenBits.bounds;
- }
- else sizeRect = qd.screenBits.bounds;
-
- windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
- if (windSize != 0)
- {
- GetPort(&oldPort);
- SetPort(theWindow);
- EraseRect(&theWindow->portRect);
- SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
- InvalRect(&theWindow->portRect);
- SetPort(oldPort);
-
- if (thisWindowIsOurs) /* update window bounds in window data struct */
- {
- if (CallDispatchProc(theData, kGrow, 0L)==kFailure)
- {
- SetWindowData_windowBounds(theData,
- (*(((WindowPeek)GetWindowPtr(theData))->contRgn))->rgnBBox);
- SetWindowData_windowPos(theData,
- GetWindowBoundsTopLeft((WindowDataHandle)theData));
- SetWindowData_windowPosInitted(theData, TRUE);
- }
- }
- }
- break;
- case inZoomIn: /* zoom box */
- case inZoomOut:
- /* the accepted way to track a zoom attempt */
- // if ((gInProgress) && (theWindow!=GetIndWindowPtr(kProgressWindow)))
- if (0)
- {
- SysBeep(7);
- return;
- }
-
- if (TrackBox(theWindow, theEvent.where, windowCode))
- {
- GetPort(&oldPort);
- SetPort(theWindow);
- ZoomWindow(theWindow, windowCode, FALSE);
- InvalRect(&theWindow->portRect);
- SetPort(oldPort);
- }
-
- if (thisWindowIsOurs) /* update window bounds in window data struct */
- {
- if (CallDispatchProc(theData, kZoom, 0L)==kFailure)
- {
- SetWindowData_windowBounds(theData,
- (*(((WindowPeek)GetWindowPtr(theData))->contRgn))->rgnBBox);
- SetWindowData_windowPos(theData,
- GetWindowBoundsTopLeft((WindowDataHandle)theData));
- SetWindowData_windowPosInitted(theData, TRUE);
- }
- }
- break;
- }
- }
-
- void ShutDownEnvironment(Boolean fullShutdown)
- {
- SaveThePrefs();
- if (fullShutdown)
- {
- ShutDownTheProgram(); /* program-specific cleanup */
- ShutDownTheGraphics(); /* shell-specific cleanup */
- }
- }
-